package com.icexxx.ice_browser;

import java.util.concurrent.atomic.AtomicInteger;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

public class VersionManager {
    private static AtomicInteger num = new AtomicInteger(0);
    private static String fileName = null;

    public static AtomicInteger getNum() {
        return num;
    }

    public static void setFileName(String fileName) {
        VersionManager.fileName = fileName;
        if (!FileUtil.exist(fileName)) {
            num = new AtomicInteger(-1);
            increment();
        } else {
            String readUtf8String = FileUtil.readUtf8String(fileName);
            readUtf8String = StrUtil.trim(readUtf8String);
            Integer versionInt = Convert.toInt(readUtf8String, 0);
            num = new AtomicInteger(versionInt);
        }
    }

    public static String increment() {
        num.getAndIncrement();
        if (StrUtil.isBlank(fileName)) {
            throw new RuntimeException("版本文件没有配置");
        }
        String content = num.toString();
        FileUtil.writeUtf8String(content, fileName);
        return content;
    }
}
